package stage
{
	import edu.pku.controller.util.RegionPoint;
	
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.utils.setTimeout;


	public class InfraRedSensorSubject extends SandTableSubject
	{
		public function InfraRedSensorSubject(id:String)
		{
			super(id, AppConstant.TYPE_INFRARED_SENSOR);
			var nimsprite:NimbusSprite=NimbusSprite.createNimbusSprite(AppConstant.TYPE_INFRARED_SENSOR);
			//var imageInstance:DisplayObject=new ImageProtypeClass() as DisplayObject;
			addChild(nimsprite);
		}

		public function addSecondPosition(xstep:int, ystep:int):void
		{
			//var ImageProtypeClass:Class=ResourceManager.InfraredSensor;
			//var imageInstance:DisplayObject=new ImageProtypeClass() as DisplayObject;
			var nimsprite:NimbusSprite=NimbusSprite.createNimbusSprite(AppConstant.TYPE_INFRARED_SENSOR);
			nimsprite.x=xstep;
			nimsprite.y=ystep;
			addChild(nimsprite);
			showLine();
		}
		
		private function showLine():void
		{
				var child1:DisplayObject=getChildAt(0);
				var child2:DisplayObject=getChildAt(1);
				var point1:Object={x:child1.x + child1.width / 2, y:child1.y + child1.height / 2};
				var point2:Object={x:child2.x + child2.width / 2, y:child2.y + child2.height / 2};
				graphics.clear();
				drawDashLine(point1,point2);
		}
		
		private function hideLine():void
		{
			graphics.clear();
			graphics.beginFill(0xff0000,0);
			graphics.endFill();
		}
		
		public var drawColor:uint=0xff0000;

		public function drawDashLine(fP1:Object, tP1:Object):void
		{
			var g:Graphics=this.graphics;
			g.lineStyle(1,0xff0000);
			if (!fP1)
			{
				fP1={x: 0, y: 0,radius:5}
			}
			if (!tP1)
			{
				tP1={x: 0, y: 0,radius:5}
			}
			var fP:Object=null;
			var tP:Object=null;
			
			if(fP1.x>tP1.x)
			{
				fP=tP1;
				tP=fP1;
			}
			else
			{
				fP=fP1;
				tP=tP1;
			}			
			
			//applyLineStyle(vedge);

			var solidLength:Number=5;
			var brokenLength:Number=2;

			var lineAngle:Number;
			lineAngle=Math.atan2(tP.y - fP.y, tP.x - fP.x);

			var xSolidLength:Number=solidLength * Math.cos(lineAngle);
			var ySolidLength:Number=solidLength * Math.sin(lineAngle);

			var xBrokenLength:Number=brokenLength * Math.cos(lineAngle);
			var yBrokenLength:Number=brokenLength * Math.sin(lineAngle);

			var forwardFlag:Boolean=true;
			if (tP.y < fP.y)
			{
				forwardFlag=false;
			}
			var tempP:Object={x: fP.x, y: fP.y};
			var tempToP:Object={x: tP.x, y: tP.y};
			var lineType:String="solid";
			while (checkIn(tempP, tP, forwardFlag))
			{
				g.beginFill(drawColor);
				if (lineType == "solid")
				{
					tempToP.x=tempP.x + xSolidLength;
					tempToP.y=tempP.y + ySolidLength;
					g.moveTo(tempP.x, tempP.y);
					if(checkIn(tempToP,tP,forwardFlag))
					{
						g.lineTo(tempToP.x, tempToP.y);
					}
					else
					{
						g.lineTo(tP.x,tP.y);
					}
					lineType="space";
				}
				else
				{
					tempToP.x=tempP.x + xBrokenLength;
					tempToP.y=tempP.y + yBrokenLength;
					g.moveTo(tempToP.x, tempToP.y);
					lineType="solid";
				}
				tempP.x=tempToP.x;
				tempP.y=tempToP.y;
				g.endFill();
			}
		}

		private function checkIn(p:Object, toP:Object, forwardFlag:Boolean):Boolean
		{
			return forwardFlag ? p.x+p.y <= toP.x+toP.y : p.x-p.y <= toP.x-toP.y;
		}
		

		protected var interval:uint=0;

		private var  lineHide:Boolean=false;
		
		public override function EffectNimbus():void
		{
			var child1:NimbusSprite=getChildAt(0) as NimbusSprite;
			var child2:NimbusSprite=getChildAt(1) as NimbusSprite;
			child1.effectNimbus();
			child2.effectNimbus();
		}
		
		public override function blink():void
		{
			super.blink();
			var instance:InfraRedSensorSubject=this;
			if(!lineHide)
			{
				hideLine();
				lineHide=true;
				setTimeout(function():void
				{
					showLine();
					instance.lineHide=false;
				},300);
			}
		}

		public override function getDrawnLinePoint():RegionPoint
		{
			var child1:DisplayObject=getChildAt(0);
			var child2:DisplayObject=getChildAt(1);
			var p:RegionPoint=new RegionPoint();
			p.x=(child1.x + child1.width / 2 + child2.x + child2.width / 2) / 2;
			p.y=(child1.y + child1.height / 2 + child2.y + child2.height / 2) / 2;
			p.radius=0;
			return p;
		}
	}
}